<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 85%; overflow-y: hidden"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model">
        <a-form-model-item label="合作意向" required prop="business">
          <a-input v-model="model.business" placeholder="请输入合作意向" />
        </a-form-model-item>

        <a-form-model-item label="联系人" required prop="name">
          <a-input v-model="model.name" placeholder="请输入联系人" />
        </a-form-model-item>

        <a-form-model-item label="手机号" required prop="contact">
          <a-input v-model="model.contact" placeholder="请输入手机号" />
        </a-form-model-item>

        <a-form-model-item label="微信二维码" required prop="wxEwm">
          <j-image-upload class="avatar-uploader" text="上传" v-model="model.wxEwm"></j-image-upload>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { addBusiness, editBusiness } from '@/api/api'
export default {
  name: 'BusinessModal',
  data() {
    return {
      title: '操作',
      visible: false,
      disableSubmit: false,
      model: {},
      layout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 14 },
      },
      confirmLoading: false,
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.visible = true
      this.edit(this.modelDefault)
    },
    edit(record) {
      console.log('record', record)
      this.model = Object.assign({}, this.defaultModel, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      console.log('model', that.model)
      const params = this.model
      console.log('params', params)
      let obj
      if (!that.model.id) {
        obj = addBusiness(params)
      } else {
        obj = editBusiness(params)
      }
      obj.then((res) => {
        if (res.code === 20000) {
          that.$message.success('添加成功！')
          that.close()
          that.$emit('ok')
        } else {
          that.$message.warning('添加失败！')
          that.close()
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style scoped>
</style>